#-*-coding:utf-8-*-

from django.utils.html import mark_safe
from django import template
import urllib, time


register = template.Library()

@register.simple_tag
def get_cpu_kernels(server):
    return server.cpu_count * server.cpu.kernel

@register.simple_tag
def get_url(request):
    return mark_safe(request.get_full_path())

@register.simple_tag
def get_disk_array(disks):
    script = 'var disks = ['
    for disk in disks:
        script = script + '{' + 'id:"' + str(disk.id) + '",name:"' + disk.name + '",mount_path:"' + disk.mount_path + '",size:"' + str(disk.size) + '"},'
    script = script + ']'
    return mark_safe(script)

@register.simple_tag
def get_ip_array(ips):
    script = 'var ips = ['
    for ip in ips:
        script = script + '{' + 'id:"' + str(ip.id) + '",ip:"' + ip.ip + '",type:"' + str(ip.type) + '"},'
    script = script + ']'
    return mark_safe(script)

@register.simple_tag
def get_datetime(add_time):
    return add_time.strftime('%Y-%m-%d')

@register.simple_tag
def get_zabbix_item_url(hostid, host):
    host = host.replace(" ", "_")
    host = host.replace('.', '_')
    return urllib.quote('/zabbix/item/' + hostid + '/' + host + '/')

@register.simple_tag
def judge_userid(user, selected_user):
    if not selected_user:
        return mark_safe('<option value="' + str(user.id) + '">' + user.username + '</option>')
    if user.id == int(selected_user):
        return mark_safe('<option value="'+ str(user.id) +'" selected="selected">'+ user.username + '</option>')
    else:
        return mark_safe('<option value="' + str(user.id) +'">' + user.username + '</option>')

@register.simple_tag
def judge_server(server, selected_server):
    if server.id in selected_server:
        return mark_safe('<option value="' + str(server.id) + '" selected="selected">' + server.name + '</option>')
    else:
        return mark_safe('<option value="' + str(server.id) + '">' + server.name + '</option>')


@register.simple_tag
def get_saltcmd_type(t):
    if t == 0:
        return u'命令'
    if t == 1:
        return u'shell脚本'
    if t == 2:
        return u'SaltSate'
    if t == 3:
        u'文件传输'

@register.simple_tag
def get_shell(context):
    return mark_safe(context)